package com.zktr.dao;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.zktr.pojo.Goods;
import com.zktr.utils.CRUDDAO;

public class GoodsDao {
	CRUDDAO<Goods> crud = new CRUDDAO<>(Goods.class);   
	/**测试
	 * @param args
	 */
	public static void main(String[] args) {
		Goods gBeans = new Goods();
		GoodsDao goodsDao = new GoodsDao();
		//gBeans.setGoodsid(10);
		gBeans.setGoodsname("小");
	//	System.out.println(goodsDao.deleteOneGoods(1102));
		System.out.println(goodsDao.queryOneGoodsId(1105));
/*		gBeans.setBrandid(4);
		gBeans.setCatid(4);
		gBeans.setGoodsid(8);
		gBeans.setGoodssn("4");
		gBeans.setGoodsname("小米");
		gBeans.setIntegral(200);
		gBeans.setBrandid(4);
		gBeans.setGoodsnumber(100);

		gBeans.setGoodsbrief("100");
		gBeans.setGoodsdesc("100");
		gBeans.setGoodsimg("img/1.png");
		gBeans.setGiveintegral(200);
		gBeans.setAddtime(1774963592);
		gBeans.setTypeid(4);
		gBeans.setIsdelete(true);
		gBeans.setIsnew(true);
		gBeans.setIshot(true);
		int updateGoods = goodsDao.insertOneGoods(gBeans);
		System.out.println("影响"+updateGoods);*/
	}
	/**
	 * 鏌ヨ鎵�鏈夋暟鎹�
	 * @return
	 */
	/**查询所有商品信息
	 * @return
	 */
	public List<Goods> queryAllGoods(){
    	   String sql="select * from Goods";
    	   LinkedHashMap<String, Object[]> map = new LinkedHashMap<String,Object[]>();
    	   map.put(sql, null);
    	   return crud.selectAll(map).get(0);
    	   
       }

	/**
	 * 查询单个商品信息
	 * @return
	 */
	public List<Goods> queryOneGoods(){
 	   String sql="select * from Goods where goodsid=?";
 	   LinkedHashMap<String, Object[]> map = new LinkedHashMap<String,Object[]>();
 	   map.put(sql, null);
 	   return crud.selectAll(map).get(0);
 	   
    }
	public int  selectGoodsCount() {
		return crud.selectCount("select count(*) from Goods");
	}
	public  List<Goods> selectGoods(int pc,int ps) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("select * from Goods LIMIT ?,?",new Object[]{(pc-1)*ps,ps});
		return 	crud.selectAll(map).get(0);
	}

	public int  selectGoodsCount(int catid) {
		return crud.selectCount("select count(*) from Goods where catid=?",catid);
	}
	/**
	 * 更新商品信息
	 * @param goods
	 * @param id
	 * @return
	 */
	public int updateGoods(Goods goods,int id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("update goods set goodssn=?,marketprice=?,goodsnumber=?,keywords=?,isnew=?,ishot=?,giveintegral=? where goodsid=?", 
				new Object[]{goods.getGoodssn(),goods.getMarketprice(),goods.getGoodsnumber(),goods.getKeywords(),goods.isIsnew(),goods.isIshot(),goods.getGiveintegral(),id});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 新增商品信息
	 * @param goods
	 * @return
	 */
	public int insertOneGoods(Goods goods) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("insert into goods values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", 
				new Object[]{goods.getGoodsid(),goods.getCatid(),goods.getGoodssn(),goods.getGoodsname(),goods.getBrandid(),
						goods.getGoodsnumber(),goods.getMarketprice(),goods.getWarnnumber(),goods.getKeywords(),goods.getGoodsbrief()
						,goods.getGoodsdesc(),goods.getGoodsimg(),goods.getIntegral(),goods.getAddtime(),goods.isIsdelete(),
						goods.isIsnew(),goods.isIshot(),goods.getTypeid(),goods.getGiveintegral()});
		 return crud.updateAll(map)[0];
	}
	/**
	 * 查询单个商品名称
	 * @param name
	 * @return
	 */
	public Goods queryOneGoods(String name){
		String sql="select * from goods where goodsname=?";
   	   LinkedHashMap<String, Object[]> map = new LinkedHashMap<String,Object[]>();
   	   map.put(sql,new Object[]{name});
   	   return crud.selectAll(map).get(0).get(0);

	}
	/**
	 * 查询单个商品Id
	 * @param name
	 * @return
	 */
	public List<Goods> queryOneGoodsId(int id){
		String sql="select * from goods where goodsid=?";
   	   LinkedHashMap<String, Object[]> map = new LinkedHashMap<String,Object[]>();
   	   map.put(sql,new Object[]{id});
   	   return crud.selectAll(map).get(0);

	}
	/**
	 * 根据分类id分页查询所有商品
	 * @return 
	 */
	public List<Goods> selectByCategoryAll(int catid,int pc,int ps) {
		return crud.select("select * from Goods where catid=? LIMIT ?,?",catid,(pc-1)*ps,ps);

	}
	public List<Goods> selectIndex() {
		return crud.select("select * from goods where goodsbrief='1'");
	}

	public int deleteOneGoods(int id) {
		Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
		map.put("delete from goods where goodsid=? ",new Object[]{id});
		 return crud.updateAll(map)[0];
		
	}
	/**
	 * 带条件的分页查询
	 * @param goods
	 * @param pc
	 * @param ps
	 * @return
	 */
	public Map<Integer, List<Goods>> selectGoods(Goods goods, int pc, int ps) {
		try {
			Map<String,Object []> map=new LinkedHashMap<String,Object []> ();
			StringBuilder sql = new StringBuilder("select * from goods");
			StringBuilder limitSql = new StringBuilder("limit ?,?");
			List<Object> params = sqlCondition(sql,goods);
			params.add((pc-1)*ps);
			params.add(ps);
			sql.append(limitSql);
			String newsql=sql.toString();
			map.put(newsql,params.toArray());
			return crud.selectAll(map);
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 带条件的查询总数
	 * @param goods
	 * @return
	 */
	public int selectGoodsCount(Goods goods) {
		StringBuilder cntSql = new StringBuilder("select count(*) from Goods");
		List<Object> params = sqlCondition(cntSql,goods);
		return crud.selectCount(cntSql.toString(),params.toArray());
	}
	/**
	 * 带条件分页查询，sql动态拼接
	 * @param cntSql
	 * @param goods
	 * @return
	 */
	private List<Object> sqlCondition(StringBuilder cntSql, Goods goods){
		List<Object> params = new ArrayList<Object>();
		StringBuilder whereSql = new StringBuilder(" where 1=1 and (");
		
		/**
		 *动态拼接语句：
		 */
		  int goodsid = goods.getGoodsid();
		if(goodsid>0) {
			whereSql.append(" or goodsid like ? ");
			params.add("%" + goodsid + "%");
		}
		String goodsname = goods.getGoodsname();
		if(goodsname != null && !goodsname.trim().isEmpty()) {
			whereSql.append("or goodsname like ? ");
			params.add("%" + goodsname + "%");
		}
		
		whereSql.delete(16, 19).append(")");
		cntSql.append(whereSql.toString());
		return params;
	}
	
}
